﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InChatter.Client.Service
{
    public class ClientContext : INotifyPropertyChanged
    {
        static ClientContext _Instance = new ClientContext();
        public static ClientContext Current
        {
            get
            {
                return _Instance;
            }
            set
            {
                if (!_Instance.Equals(value))
                {
                    if (_Instance != null && _Instance.IsOnline)
                    {
                        ChatMsgCenter.Instance.Logout();
                    }
                    _Instance = value;
                    if (value != null)
                    {
                        ChatMsgCenter.Instance.Login();
                    }
                    OnClientChanged();
                }
            }
        }

        public static ObservableCollection<ClientContext> AllClients = new ObservableCollection<ClientContext>();
        public string ClientId { set; get; }
        public string ClientName { set; get; }

        public ClientContext()
            : this("", "", false)
        {
        }

        public ClientContext(string clientId, string clientName)
            : this(clientId, clientName, false)
        {

        }

        public ClientContext(string clientId, string clientName, bool isOnline)
        {
            this.ClientId = clientId;
            this.ClientName = clientName;
            this.IsOnline = isOnline;
        }

        private bool _IsOnline;
        /// <summary>
        /// the client's online state
        /// </summary>
        public bool IsOnline
        {
            get { return _IsOnline; }
            set
            {
                _IsOnline = value;
                OnPropertyChanged("IsOnline");
            }
        }

        private int _Count;
        /// <summary>
        /// the client's unread message count
        /// </summary>
        public int Count
        {
            get { return _Count; }
            set
            {
                _Count = value;
                OnPropertyChanged("Count");

            }
        }

        public override bool Equals(object obj)
        {
            ClientContext client = obj as ClientContext;
            if (client!=null && this.ClientId == client.ClientId)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        #region ClientChanged Event
        public static event Action ClientChanged;
        public static void OnClientChanged()
        {
            if (ClientChanged != null)
            {
                OnClientChanged();
            }
        }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}
